﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PasswordControl
{
    public static class DataSerialize
    {
        public static bool SaveData<T>(T data, string path)
        {
            try
            {
                using (TextWriter textWriter = new StreamWriter(path))
                {
                    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                    serializer.Serialize(textWriter, data);
                    textWriter.Close();
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static T LoadData<T>(string path)
        {
            T data = default(T);
            try
            {
                using (TextReader textReader = new StreamReader(path))
                {
                    System.Xml.Serialization.XmlSerializer deserializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                    data = (T)deserializer.Deserialize(textReader);
                    textReader.Close();
                }
            }
            catch (Exception)
            {
            }
            return data;
        }

        public static bool SaveEncrypted<T>(T data, string path, string password)
        {
            try
            {
                //serialize the data to a string using a memory stream 
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                MemoryStream ms = new MemoryStream();
                serializer.Serialize(ms, data);
                ms.Seek(0, SeekOrigin.Begin);
                StreamReader rdr = new StreamReader(ms);
                string str = rdr.ReadToEnd();

                //Encrypt the data string
                str = Encryption.Encrypt(str, password);

                //write the data string to a file
                FileMode filemode;
                if (File.Exists(path))
                    filemode = FileMode.Truncate;
                else
                    filemode = FileMode.CreateNew;
                FileStream file = new FileStream(path, filemode, FileAccess.Write);
                StreamWriter sw = new StreamWriter(file);
                sw.Write(str);
                sw.Close();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static T LoadEncrypted<T>(string path, string password)
        {
            T data = default(T);
            try
            {
                //read from file to string
                FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(file);
                string str = sr.ReadToEnd();
                sr.Close();
                file.Close();

                //Encrypt the string
                str = Encryption.Decrypt(str, password);
                if (str == null) return data;

                //serialize the string into Owner_Class using a memory stream
                System.Xml.Serialization.XmlSerializer deserializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                byte[] byteArray = Encoding.ASCII.GetBytes(str);
                MemoryStream ms = new MemoryStream(byteArray);
                data = (T)deserializer.Deserialize(ms);
            }
            catch (Exception)
            {
            }
            return data;
        }
    }
}
